package com.ztewatch.wearcontacts;

import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri.Builder;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Data;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CursorAdapter;

import com.ztewatch.wearcontacts.loader.ContactsListLoader;
import com.ztewatch.wearcontacts.view.ContactListAdapter;
import com.ztewatch.wearcontacts.view.ScrollGridView;

public class WearContactSelectionActivity extends Activity {
	private static final String EXTRA_PHONE_URIS = "com.android.contacts.extra.PHONE_URIS";
	private ScrollGridView contact_gridview;
	private CursorAdapter contactCursorAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wear_contact_selection);
		initContactSelectGridView();
	}

	private void initContactSelectGridView() {
		contact_gridview = (ScrollGridView) findViewById(R.id.contact_select_gridview);
		contactCursorAdapter = new ContactListAdapter(this, null);
		contact_gridview.setAdapter(contactCursorAdapter);
		contact_gridview.setOnItemClickListener(new ContactItemClickListener());
	}

	private class ContactItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			if (position == 0) {// History
				return;
			}
			Cursor cursor = (Cursor) contactCursorAdapter.getItem(position);
			if (cursor != null) {
				String number = cursor.getString(ContactsListLoader.NUMBER_COLUMN_ID);
				Intent intent = new Intent();
				intent.putExtra(EXTRA_PHONE_URIS, number);
//				intent.putExtra("numbers_array", true);
				setResult(RESULT_OK, intent);
				finish();
			}
		}

	}

	private final LoaderCallbacks<Cursor> loaderCallbacks = new LoaderCallbacks<Cursor>() {

		@Override
		public void onLoaderReset(Loader<Cursor> arg0) {

		}

		@Override
		public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
			contactCursorAdapter.swapCursor(arg1);
		}

		@Override
		public Loader<Cursor> onCreateLoader(int id, Bundle args) {
			final Builder builder = Data.CONTENT_URI.buildUpon();
			StringBuilder selection = new StringBuilder();
			selection.append(Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE
					+ "','" + Email.CONTENT_ITEM_TYPE + "')");
			builder.appendQueryParameter(
					ContactsContract.REMOVE_DUPLICATE_ENTRIES, "true");
			ContactsListLoader contactsListLoader = new ContactsListLoader(WearContactSelectionActivity.this);
			contactsListLoader.setUri(builder.build());
			contactsListLoader.setSelection(selection.toString());
			contactsListLoader.setProjection(ContactsListLoader.CONTACTS_SUMMARY_PROJECTION);
			contactsListLoader.setSortOrder(Data.DISPLAY_NAME_PRIMARY
							+ " COLLATE LOCALIZED ASC");
			return contactsListLoader;
		}
	};

	@Override
	protected void onStart() {
		super.onStart();
		getLoaderManager().restartLoader(0, null, loaderCallbacks);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		getLoaderManager().destroyLoader(0);
	}
}
